package com.example.examination;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;

public class MainActivity extends AppCompatActivity {
    private TextView tv_gcs;
    private TextView tv_jsy;
    private TextView tv_czy;
    private TextView tv_zjy;
    private Button bu_xyt;
    private List<MainActivity_list> mainActivity_lists = new ArrayList<>();
    private GridView gridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initdata1();
    }

    private void initView() {
        gridView = (GridView) findViewById(R.id.gridView);
        bu_xyt=(Button)findViewById(R.id.bu_xyt);
        bu_xyt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,MainActivity2.class));
            }
        });
    }
    public interface BlogService1 {
        @POST("orderWorker")
        Call<ResponseBody> getBlog1(@Body RequestBody user);
    }

    public interface BlogService {
        @GET("getProduceInfo")
        Call<ResponseBody> getBlog();
    }

    private void initdata1() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://hh1.me:5001/")
                .build();
        BlogService service = retrofit.create(BlogService.class);
        Call<ResponseBody> call = service.getBlog();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Gson gson2 = new Gson();
                try {
                    MainActivity_list carlist2 = gson2.fromJson(response.body().string(), MainActivity_list.class);
                    mainActivity_lists.add(carlist2);
                    gridView.setAdapter(new MyAdapter());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }
    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mainActivity_lists.get(0).getData().size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            View view1=(View)View.inflate(MainActivity.this,R.layout.activity_main1 ,null);
            tv_gcs = (TextView)view1. findViewById(R.id.tv_gcs);
            tv_jsy = (TextView) view1.findViewById(R.id.tv_jsy);
            tv_czy = (TextView) view1. findViewById(R.id.tv_czy);
            tv_zjy = (TextView)view1.  findViewById(R.id.tv_zjy);
            tv_gcs.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                        String s = i+"";
                        String s1 = "gcs";
                    mainActivity_lists = new ArrayList<>();
                    initdata(s,s1);
                }
            });
            tv_jsy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String s = i+"";
                    String s1 = "jsy";
                    mainActivity_lists = new ArrayList<>();
                    initdata(s,s1);
                }
            });
            tv_zjy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String s = i+"";
                    String s1 = "zjy";
                    mainActivity_lists = new ArrayList<>();
                    initdata(s,s1);
                }
            });
            tv_czy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String s = i+"";
                    String s1 = "czy";
                    mainActivity_lists = new ArrayList<>();
                    initdata(s,s1);
                }
            });
            tv_gcs.setText("姓名："+mainActivity_lists.get(0).getData().get(i).getGcs().getName()+"\n"+"体力："+mainActivity_lists.get(0).getData().get(i).getGcs().getPower()+"");
            tv_jsy.setText("姓名："+mainActivity_lists.get(0).getData().get(i).getJsy().getName()+"\n"+"体力："+mainActivity_lists.get(0).getData().get(i).getJsy().getPower()+"");
            tv_czy.setText("姓名："+mainActivity_lists.get(0).getData().get(i).getCzy().getName()+"\n"+"体力："+mainActivity_lists.get(0).getData().get(i).getCzy().getPower()+"");
            tv_zjy.setText("姓名："+mainActivity_lists.get(0).getData().get(i).getZjy().getName()+"\n"+"体力："+mainActivity_lists.get(0).getData().get(i).getZjy().getPower()+"");
            return view1;
        }
    }

    private void initdata(String s, String s1) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://hh1.me:5001/")
                .build();
        Map<String, String> map = new HashMap<>();
        map.put("pos", s);
        map.put("gangwei",s1);
        Gson gson = new Gson();
        RequestBody requestBody = RequestBody.create(gson.toJson(map), MediaType.parse("application/json;charset=utf-8"));
        BlogService1 service = retrofit.create(BlogService1.class);
        Call<ResponseBody> call = service.getBlog1(requestBody);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Toast.makeText(MainActivity.this, "替补成功", Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
            }
        });
        initdata1();
    }
}
